package com.xmy.cultivate.listeners;

import com.aliyun.facebody20191230.models.AddFaceResponse;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xmy.cultivate.constants.MqConstants;
import com.xmy.cultivate.entity.*;
import com.xmy.cultivate.entity.views.StudyPlan;
import com.xmy.cultivate.respons.ResStudentAccount;
import com.xmy.cultivate.service.*;
import com.xmy.cultivate.util.Code;
import com.xmy.cultivate.util.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;

import java.text.ParseException;
import java.util.List;
import java.util.Objects;
import java.util.Stack;

@Slf4j
@Component
public class StudentAccountListener {


    @Autowired
    @Lazy
    IStudentAccountService iStudentAccountService;

    @Autowired
    @Lazy
    IStudentGradeService iStudentGradeService;

    @Autowired
    @Lazy
    IRenewDataService iRenewDataService;

    @Autowired
    @Lazy
    ILackCourseLogService iLackCourseLogService;

    @Autowired
    @Lazy
    IStudentLessonNumService iStudentLessonNumService;

    @Autowired
    @Lazy
    ICourseSchedulingService iCourseSchedulingService;

    @Autowired
    @Lazy
    IOrderDetailService iOrderDetailService;

    String faceDbname;
    @Value("${uploadFile.resourceHandler}")

    @Autowired
    @Lazy
    IStudentService iStudentService;

    @Autowired
    @Lazy
    IAliyunFaceService iAliyunFaceService;

    @Autowired
    @Lazy
    IStudentFaceDataService iStudentFaceDataService;

    @Autowired
    IStaffService iStaffService;

    /**
     * 添加学生帐号
     *
     * @param studentIdAndSubjects
     * @throws InterruptedException
     */
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = MqConstants.QUEUE_CULTIVATE_ADD_STUDENT_ACCOUNT_Q1, durable = "true"),
            exchange = @Exchange(name = MqConstants.EXCHANGE_CULTIVATE_ADD_STUDENT_ACCOUNT_FANOUT, type = ExchangeTypes.FANOUT)
    ))
    public void listenDirectQueueAddAccount(String studentIdAndSubjects) throws InterruptedException {
        //System.out.println("消费者english 收到了 direct.queue1的消息：【" + studentId +"】");
        String[] studentIdAndSubjectsArr = studentIdAndSubjects.split("-"); // ,年份，学期，学生id:科目id
        Integer len = studentIdAndSubjectsArr.length;
        if (len == 4) {
            Integer yearPart = Integer.parseInt(studentIdAndSubjectsArr[0]);
            Integer quarterNum = Integer.parseInt(studentIdAndSubjectsArr[1]);
            Long studentId = Long.parseLong(studentIdAndSubjectsArr[2]);
            Long subjectsId = Long.parseLong(studentIdAndSubjectsArr[3]);
            iStudentAccountService.addStudentAccountBatch(yearPart, quarterNum, studentId, subjectsId);
        }

    }

    /**
     * 英语端修改学生帐号信息，同步过来财务
     *
     * @param studentAccount
     */
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = MqConstants.QUEUE_ERP_UPDATE_PWD_STUDENT_ACCOUNT_Q2, durable = "true"),
            exchange = @Exchange(name = MqConstants.EXCHANGE_ERP_UPDATE_PWD_STUDENT_ACCOUNT_FANOUT, type = ExchangeTypes.FANOUT)
    ))
    public void updateStudentAccountPwd(StudentAccount studentAccount) {
        iStudentAccountService.updateStudentAccountPwd(studentAccount);
    }

    /**
     * 更新最后一次分班信息标识
     *
     * @param studentGrade
     */
//    @RabbitListener(bindings = @QueueBinding(
//            value = @Queue(name = MqConstants.QUEUE_ERP_STUDENT_GRADE_LAST_Q1, durable = "true"),
//            exchange = @Exchange(name = MqConstants.EXCHANGE_ERP_STUDENT_GRADE_LAST_FANOUT, type = ExchangeTypes.FANOUT)
//    ))
//    public void updateStudentGradeLast(StudentGrade studentGrade) {
//        iStudentGradeService.updateIsLast(studentGrade);
//    }

    /**
     * 更新续班信息教师
     *
     * @param studentGrade
     */
//    @RabbitListener(bindings = @QueueBinding(
//            value = @Queue(name = MqConstants.QUEUE_ERP_UPDATE_RENEW_TEACHER_Q1, durable = "true"),
//            exchange = @Exchange(name = MqConstants.EXCHANGE_ERP_UPDATE_RENEW_TEACHER_FANOUT, type = ExchangeTypes.FANOUT)
//    ))
//    public void updateReNewTeacher(StudentGrade studentGrade) {
//        iRenewDataService.updateTeacher(studentGrade.getSchoolId(), studentGrade.getStudentId(), studentGrade.getCourseId(), studentGrade.getGradeId());
//    }


    /**
     * //添加缺课数据
     *
     * @param lackCourseLog
     */
//    @RabbitListener(bindings = @QueueBinding(
//            value = @Queue(name = MqConstants.QUEUE_ERP_ADD_LACK_COURSE_lOG_Q1, durable = "true"),
//            exchange = @Exchange(name = MqConstants.EXCHANGE_ERP_ADD_LACK_COURSE_lOG_FANOUT, type = ExchangeTypes.FANOUT)
//    ))
//    public void lackCourseLogBatchAdd(LackCourseLog lackCourseLog) {
//        iLackCourseLogService.batchAdd(lackCourseLog.getYearPart(), lackCourseLog.getQuarterNum(), lackCourseLog.getSchoolId(), lackCourseLog.getStudentId(), lackCourseLog.getSubjectsId(), lackCourseLog.getYearClassId(), lackCourseLog.getGradeId(), lackCourseLog.getType(), lackCourseLog.getLackType());
//    }

    /**
     * 添加学生课次
     *
     * @param studentLessonNum
     */
//    @RabbitListener(bindings = @QueueBinding(
//            value = @Queue(name = MqConstants.QUEUE_ERP_ADD_STUDENT_LESSON_Q1, durable = "true"),
//            exchange = @Exchange(name = MqConstants.EXCHANGE_ERP_ADD_STUDENT_LESSON_FANOUT, type = ExchangeTypes.FANOUT)
//    ))
//    public void addStudentLesson(StudentLessonNum studentLessonNum) {
//        iStudentLessonNumService.addStudentLesson(studentLessonNum.getGradeId(), studentLessonNum.getStudentId());
//    }


    /**
     * 判断是否通关，已通关不算应到
     */
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = MqConstants.QUEUE_EXCHANGE_CHA_STUDY_STATUS_Q1, durable = "true"),
            exchange = @Exchange(name = MqConstants.EXCHANGE_ERP_CHA_STUDY_STATUS_FANOUT, type = ExchangeTypes.FANOUT)
    ))
    public void chaStudyPlanAdd(ChaStudyPlan studyPlan) {
        iStudentLessonNumService.chaStudyPlanAdd(studyPlan);
    }

    /**
     * 财务后台创建完老师账号同步到培训系统中
     */
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = MqConstants.QUEUE_CULTIVATE_ADD_STAFF_Q1, durable = "true"),
            exchange = @Exchange(name = MqConstants.EXCHANGE_CULTIVATE_ADD_STAFF_FANOUT, type = ExchangeTypes.FANOUT)
    ))
    public void addStaff(Staff staff) throws InterruptedException {
        if (staff != null) {
            iStaffService.saveTo(staff);
        }
    }

    /**
     * 财务后台创建完老师账号同步到培训系统中
     */
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = MqConstants.QUEUE_CULTIVATE_UPDATE_STAFF_Q1, durable = "true"),
            exchange = @Exchange(name = MqConstants.EXCHANGE_CULTIVATE_UPDATE_STAFF_FANOUT, type = ExchangeTypes.FANOUT)
    ))
    public void updateStaff(Staff staff) throws InterruptedException {
        if (staff != null) {
            Staff staffInfo = iStaffService.getById(staff.getId());
            if(Objects.isNull(staffInfo)){
                iStaffService.saveTo(staff);
            }else {
                staff.setAdminId(staff.getId());
                iStaffService.updateTo(staff);
            }

        }
    }
}
